#coding:utf-8

import requests
import time
import json
from mysql import sqlExecute

"""
获取省份top5的疫情
"""

def get_url():
    # 抓取数据
    ## 先把数据都爬下来，查看数据结构，明确要整理保存的数据
    list = []
    # url_1包含中国各省市当日实时数据
    url_1 = 'https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'
    list.append(url_1)
    return list

def main():
    getData()

    # result = saveData(datalist)
    # if result is None:
    #     print("数据存储失败")
    # else:
    #     print("数据存储成功")

#第一步：爬取网页数据
def getData():
    url1 = get_url()[0]
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36"}
    result = requests.get(url=url1, headers=header)
    data = json.loads(result.json()['data'])
    print(data['provinceCompare'])

    str = "/html/body/div[2]/div/div/div/section/div[2]/div[4]/div[3]/div[4]/div/table/tbody/tr[1]"

# 第二步： 将数据保存到数据库中
def saveData(datalist):
    sqlDb = sqlExecute.MysqlDb()
    sql = "insert into covid_province(province_name,today_confirm,today_confirmCuts,total_nowConfirm,total_confirm,total_suspect,total_dead,total_deadRate,total_heal,total_healRate) " \
          "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    result = sqlDb.execute_sql(sql,datalist)
    return result

if __name__ == "__main__":
    main()

